#import "RtkOtaPlugin.h"
#import "BleTool.h"
#import "UnzipFirmware.h"
#import <CommonCrypto/CommonDigest.h>


@implementation RtkOtaPlugin

static FlutterMethodChannel* channel;

NSString *filePath;

NSString *colorPath;

NSString *deviceName;


+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
   channel = [FlutterMethodChannel
      methodChannelWithName:@"rtk_ota_plugin"
            binaryMessenger:[registrar messenger]];
  RtkOtaPlugin* instance = [[RtkOtaPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if([@"initIosDfuAdapter" isEqualToString:call.method]){
      NSString *path = @"";
      path = [call.arguments objectForKey:@"path"];
      [self setFilePath:path];
      deviceName = [call.arguments objectForKey:@"name"];
      [self initDfuAdapter];
      result(@"");
  }else if([@"setFilePath" isEqualToString:call.method]){
      NSString *path = @"";
      path = [call.arguments objectForKey:@"path"];
      [self setFilePath:path];
      result(@"");
  }else if([@"md5" isEqualToString:call.method]){
      NSString *path = @"";
      path = [call.arguments objectForKey:@"path"];
      NSString *md5 = [self getFileMD5StrFromPath:path];
      if([md5 isEqualToString:@"file is null"]){
          result(@"1,file is null");
      }else{
          result([NSString stringWithFormat:@"2,%@",md5]);
      }
      
  }else if([@"getColorPath" isEqualToString:call.method]){
      if(colorPath){
          result(colorPath);
      }else{
          result(@"");
      }
      
  }else {
    result(FlutterMethodNotImplemented);
  }
}

- (NSString *)getFileMD5StrFromPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path isDirectory:nil])
    {
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        unsigned char digest[16];
        
        CC_MD5( data.bytes, (CC_LONG)data.length-16, digest );
        
        NSMutableString *output = [NSMutableString stringWithCapacity:16 * 2];
        
        for( int i = 0; i < 16; i++ )
        {
            [output appendFormat:@"%02x", digest[i]];
        }
        return output;
    }
    else
        return @"file is null";
}

- (void)BleToolProgress:(NSString *)progress {
    if(channel){
        [channel invokeMethod:@"sendMessage" arguments:progress];
    }
}

-(void)initDfuAdapter{
    [BleTool sharedManager].delegate = self;
    [[BleTool sharedManager] setDeviceName:deviceName];
    [[BleTool sharedManager] initData];
}

- (void)setFilePath:(NSString *)path {
    UnzipFirmware *unzipFiles = [[UnzipFirmware alloc] init];
    NSURL *url = [[NSURL alloc] initWithString:path];
    NSArray *firmwareFilesURL = [unzipFiles unzipFirmwareFiles:url];
    for (NSURL *url in firmwareFilesURL) {
        NSString *strPath = url.path;
        NSLog(@"strPaht:%@",strPath);
        if([[strPath lowercaseString] containsString:@"app_mp_sdk"]){
            filePath = strPath;
            [[BleTool sharedManager] setFilePath:strPath];
        }
        if([[strPath lowercaseString] containsString:@"color565"]){
            colorPath = strPath;
        }
    }
}

@end
